package com.dg.gaming.tictactoe.gui
{
	import com.dg.gaming.api.common.util.Color;
	import com.dg.gaming.api.common.util.TextDraw;
	import com.dg.gaming.client.gui.AbstractBoardComponent;
	import com.dg.gaming.client.gui.GameImages;
	import com.dg.gaming.client.gui.GlassPane;
	import com.dg.gaming.tictactoe.common.TicTacToeModel;

	import flash.display.BitmapData;
	import flash.geom.Point;

	public class TicTacToeBoardComponent extends AbstractBoardComponent
	{
		private static const NUM_OF_ROWS_COLS:int=3;

		private static const CELL_SIZE:int=65;

		private static const CELL_SPACING:int=1;

		private var X_OR_0:Boolean=false;

		private var _model:TicTacToeModel;

		private var grassPane:GlassPane=new GlassPane();

		private var controller:TicTacToeController;
		private var td:TextDraw;


		public function setControllerObject(controller:TicTacToeController):void
		{
			this.controller=controller;
		}

		public function TicTacToeBoardComponent(model:TicTacToeModel)
		{
			super(NUM_OF_ROWS_COLS, NUM_OF_ROWS_COLS, CELL_SIZE, CELL_SPACING, 0, 0, false, false, false);
			_model=model;
			_model.addEventListener(Observer.OBSERVER_EVENT, update);
			grassPane.setPaneVisible(false);
			grassPane.x=0;
			grassPane.y=0;
			grassPane.percentHeight=100;
			grassPane.percentWidth=100;
			td=new TextDraw(systemManager);
			setColours(Color.white, Color.gray, Color.black, Color.white);


		}

		public function update(evnt:Observer):void
		{
			refresh();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(width, height);
			for (var x:int=0; x < 3; x++)
			{
				for (var y:int=0; y < 3; y++)
				{
					var piece:int=_model.getData(x, y);
					if (piece != TicTacToeModel.BLANK)
					{
						var screen:Point=getScreenCoords(x, y);
						var image:BitmapData=GameImages.getImageByIndex(piece + 1); // image
						td.drawImage(graphics, screen.x, screen.y, image.width, image.height, image);
					}
				}
			}
		}


		private function refresh():void
		{
			invalidateDisplayList();
		}



		public function get model():TicTacToeModel
		{
			return _model;
		}

	}
}